package soft_struct_Ex;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class SelectGUI extends JFrame{
   private JSplitPane  bigSplitPane;
   private JScrollPane showInfoPane;
   private JPanel btnPanel;
   private JComboBox cmbSqlType, cmbHouseType;
   private JLabel lblSqlType;
   private Dimension   minimumSize;
   private JTextArea txtForInfo;

   public static final String SHOW = "SURE";
   public static final String WRITE = "WRITE";
   public static final String READ = "READ";
   public static final String EXIT = "Exit";
   public static final String Oracle = "Oracle";
   public static final String MySql = "MySql";
   public static final String SQLService = "SQLService";

   public SelectGUI(){
      super("Select SQL Type");
      minimumSize = new Dimension(130, 100);
      setUpChoicePanel();
      setUpScrollPanes();
   }

   private void setUpChoicePanel(){
      cmbSqlType = new JComboBox();
	  cmbSqlType.addItem(Oracle);
	  cmbSqlType.addItem(MySql);
	  cmbSqlType.addItem(SQLService);

	  lblSqlType = new JLabel("Sql Types");

	  //Create the open button
	  JButton showInfoBtn = new JButton(SHOW);
	  JButton writeBtn = new JButton(WRITE);
	  JButton readBtn = new JButton(READ);
	  showInfoBtn.setMnemonic(KeyEvent.VK_S);
	  JButton exitButton = new JButton(EXIT);
	  exitButton.setMnemonic(KeyEvent.VK_X);

	  ButtonListener btnListener = new ButtonListener();

	  // add action Listener
	  showInfoBtn.addActionListener(btnListener);
	  exitButton.addActionListener(btnListener);
	  readBtn.addActionListener(btnListener);
	  writeBtn.addActionListener(btnListener);

	  btnPanel = new JPanel();

	  //------------------------------------------------
	  GridBagLayout gridbag = new GridBagLayout();
	  btnPanel.setLayout(gridbag);
	  GridBagConstraints gbc = new GridBagConstraints();

	  btnPanel.add(lblSqlType);
	  btnPanel.add(cmbSqlType);
	  btnPanel.add(showInfoBtn);
	  btnPanel.add(exitButton);
	  btnPanel.add(writeBtn);
	  btnPanel.add(readBtn);

      gbc.insets.top = 5;
      gbc.insets.bottom = 5;
      gbc.insets.left = 5;
      gbc.insets.right = 5;

      gbc.gridx = 0;
      gbc.gridy = 0;
      gridbag.setConstraints(lblSqlType, gbc);
      gbc.gridx = 1;
      gbc.gridy = 0;
      gridbag.setConstraints(cmbSqlType, gbc);

      gbc.insets.left = 2;
      gbc.insets.right = 2;
      gbc.insets.top = 15;
      gbc.gridx = 0;
      gbc.gridy = 5;
      gridbag.setConstraints(showInfoBtn, gbc);
      gbc.anchor = GridBagConstraints.WEST;
      gbc.gridx = 1;
      gbc.gridy = 5;
      gridbag.setConstraints(exitButton, gbc);
      gbc.gridx = 2;
      gbc.gridy = 15;
      gridbag.setConstraints(writeBtn, gbc);
      gbc.gridx = 4;
      gbc.gridy = 15;
      gridbag.setConstraints(readBtn, gbc);
      //-----------------------------------------------
   }

   private void setUpScrollPanes(){
   	  txtForInfo = new JTextArea("Here shows what you select.", 20, 30);
   	  txtForInfo.setFont(new Font("Helvetica", Font.BOLD, 15));

  	  txtForInfo.setLineWrap(true);
  	  txtForInfo.setBackground(Color.green);

  	  showInfoPane = new JScrollPane(txtForInfo);

  	  bigSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, showInfoPane, btnPanel);
  	  bigSplitPane.setDividerLocation(220);

      getContentPane().add(bigSplitPane);
  	  setSize(new Dimension(500, 300));
      setVisible(true);
   }

   class ButtonListener implements ActionListener{
      public void actionPerformed(ActionEvent ae){

		if (ae.getActionCommand().equals(EXIT)){
		    System.exit(1);
		}
		if (ae.getActionCommand().equals(SHOW)){
		   String type = (String) cmbSqlType.getSelectedItem();
		   SqlSelect pp=null;
		   String option="";

		   if (type.equals(Oracle)){
		      option="Oracle";
	
		   }
		   else if (type.equals(MySql)){
		      option="MySql";
		   }
		   else if (type.equals(SQLService)){
			      option="SQLService";
			   }
           AutoSQL ai = SqlSelect.getSelectObj(option);
		   String desc = ai.getSql();
		   txtForInfo.setText(desc);
		}
		  if (ae.getActionCommand().equals(WRITE)){
		   String type = (String) cmbSqlType.getSelectedItem();
			txtForInfo.setText("write");
			   String option="";

			   if (type.equals(Oracle)){
			      option="Oracle";	
			   }
			   else if (type.equals(MySql)){
			      option="MySql";
			   }
			   else if (type.equals(SQLService)){
				      option="SQLService";
				   }
	           AutoSQL ai = SqlSelect.getSelectObj(option);
			   txtForInfo.setText(ai.write());
		      }
		   else if (ae.getActionCommand().equals(READ)){
			   String type = (String) cmbSqlType.getSelectedItem();
				   String option="";

				   if (type.equals(Oracle)){
				      option="Oracle";	
				   }
				   else if (type.equals(MySql)){
				      option="MySql";
				   }
				   else if (type.equals(SQLService)){
					      option="SQLService";
					   }
		           AutoSQL ai = SqlSelect.getSelectObj(option);
				   txtForInfo.setText(ai.read());
			      }
        }
      }
  

   public static void main(String args[])
   {
      try {
         UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
      }
      catch (Exception evt) {}

      SelectGUI frame = new SelectGUI();
      frame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e)
         {
            System.exit(0);
         }
      }
      );
      frame.setSize(500, 420);
      frame.setVisible(true);
   }
}
